use crate::{Directory, File, ParseProject, Project};
use axum::Json;
use c2rust002::transpile_file_s;
use c2rust004::TranspileFile;

pub async fn parse_project(Json(payload): Json<ParseProject>) -> Json<ParseProject> {
    fn convert_c_to_rust_project_structure(c_project: &Project) -> Project {
        let rust_project_name = format!("{}_rust", c_project.name);

        // 创建Rust项目结构
        let rust_directories = Some(vec![Directory {
            name: "src".to_string(),
            path: "src".to_string(),
            directories: None,
            files: Some(vec![File {
                name: "main.rs".to_string(),
                content: "fn main() {\n    println!(\"Hello, world!\");\n}".to_string(),
                path: "src/main.rs".to_string(),
            }]),
        }]);

        let rust_files = Some(vec![
            File {
                name: "Cargo.toml".to_string(),
                content: format!(
                    "[package]\nname = \"{}\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\n",
                    rust_project_name
                ),
                path: "Cargo.toml".to_string(),
            }
        ]);

        Project {
            name: rust_project_name,
            directories: rust_directories,
            files: rust_files,
        }
    }

    let rust_project = convert_c_to_rust_project_structure(&payload.project);
    println!("Converted Rust project: {:?}", rust_project);
    println!("Received project: {:?}", payload);
    Json(ParseProject {
        project: rust_project,
    })
}

pub async fn transpile_project(Json(payload): Json<ParseProject>) -> Json<ParseProject> {
    fn convert_c_to_rust_project_structure(c_project: &Project) -> Project {
        let rust_project_name = format!("{}_rust", c_project.name);
        // 创建Rust项目结构
        let rust_directories = Some(vec![Directory {
            name: "src".to_string(),
            path: "src".to_string(),
            directories: None,
            files: Some(vec![File {
                name: "main.rs".to_string(),
                content: "fn main() {\n    println!(\"Hello, world!\");\n}".to_string(),
                path: "src/main.rs".to_string(),
            }]),
        }]);

        let rust_files = Some(vec![
            File {
                name: "Cargo.toml".to_string(),
                content: format!(
                    "[package]\nname = \"{}\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\n",
                    rust_project_name
                ),
                path: "Cargo.toml".to_string(),
            }
        ]);

        Project {
            name: rust_project_name,
            directories: rust_directories,
            files: rust_files,
        }
    }

    let rust_project = convert_c_to_rust_project_structure(&payload.project);
    println!("Converted Rust project: {:?}", rust_project);
    println!("Received project: {:?}", payload);
    Json(ParseProject {
        project: rust_project,
    })
}

pub async fn transpile_file(Json(payload): Json<TranspileFile>) -> Json<TranspileFile> {
    Json(transpile_file_s(payload).await)
}
